package com.icongyou.enterprise.config;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;

@Configuration
@MapperScan({
        "com.icongyou.enterprise.data_aggregation.mapper",
        "com.icongyou.enterprise.talent_analysis.mapper",
        "com.icongyou.enterprise.talent_marking.mapper",
        "com.icongyou.enterprise.talent_pool.mapper",
        "com.icongyou.enterprise.talent_profile.mapper"
})
public class MyBatisConfig {
    
    @Bean
    public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(dataSource);
        
        // 加载 MyBatis 配置文件
        factoryBean.setConfigLocation(
                new PathMatchingResourcePatternResolver().getResource("classpath:mybatis-config.xml")
        );
        
        // 扫描 TypeHandler 包
        factoryBean.setTypeHandlersPackage("com.icongyou.enterprise.config.typehandler");
        
        // 扫描 Mapper XML 文件
        factoryBean.setMapperLocations(
                new PathMatchingResourcePatternResolver().getResources("classpath*:com/icongyou/enterprise/**/mapper/*.xml")
        );
        
        return factoryBean.getObject();
    }
}